package cn.qingfeng.a003_chat_v2_1.server.handler;

import cn.qingfeng.a003_chat_v2_1.message.LoginRequestMessage;
import cn.qingfeng.a003_chat_v2_1.message.LoginResponseMessage;
import cn.qingfeng.a003_chat_v2_1.server.session.Session;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class LoginHandler extends SimpleChannelInboundHandler<LoginRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginRequestMessage message) throws Exception {
        try {
            if (message == null) {
                return;
            }

            String username = message.getUsername();
            String password = message.getPassword();
            boolean loginFlag = false;
            if ("000000".equals(password)) {
                loginFlag = true;
            }
            if (loginFlag) {
                Session.bind(username, ctx.channel());
                ctx.writeAndFlush(new LoginResponseMessage("login2", true, "登录成功"));
            } else {
                ctx.writeAndFlush(new LoginResponseMessage("login2", false, "登录失败++"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
